Spot-On
spot-on-web-server.h
1 /*
2 ** Copyright (c) 2011 - 10^10^10, Alexis Megas.
3 ** All rights reserved.
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions
7 ** are met:
8 ** 1. Redistributions of source code must retain the above copyright
9 ** notice, this list of conditions and the following disclaimer.
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
13 ** 3. The name of the author may not be used to endorse or promote products
14 ** derived from Spot-On without specific prior written permission.
15 **
16 ** SPOT-ON IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 ** SPOT-ON, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifndef _spoton_web_server_h_
29 #define _spoton_web_server_h_
30 
31 #include <QPointer>
32 #include <QTcpServer>
33 #include <QThread>
34 #include <QTimer>
35 
36 class QSslSocket;
37 
38 class spoton_web_server_tcp_server: public QTcpServer
39 {
40  Q_OBJECT
41 
42  public:
43  spoton_web_server_tcp_server(QObject *parent):QTcpServer(parent)
44  {
45  }
46 
48  {
49  }
50 
51  QByteArray certificate(void) const
52  {
53  return m_certificate;
54  }
55 
56  QByteArray privateKey(void) const
57  {
58  return m_privateKey;
59  }
60 
61  void clear(void)
62  {
63  m_certificate.clear();
64  m_privateKey.clear();
65  }
66 
67  void incomingConnection(qintptr socketDescriptor);
68 
69  void setCertificate(const QByteArray &certificate)
70  {
71  m_certificate = certificate;
72  }
73 
74  void setPrivateKey(const QByteArray &privateKey)
75  {
76  m_privateKey = privateKey;
77  }
78 
79  private:
80  QByteArray m_certificate;
81  QByteArray m_privateKey;
82 
83  signals:
84  void newConnection(const qint64 socketDescriptor);
85 };
86 
88 
89 class spoton_web_server: public QObject
90 {
91  Q_OBJECT
92 
93  public:
94  spoton_web_server(QObject *parent);
96  int httpClientCount(void) const;
97  int httpsClientCount(void) const;
98 
99  private:
100  QAtomicInt *m_abort;
101  QAtomicInt *m_httpClientCount;
102  QAtomicInt *m_httpsClientCount;
103  QPointer<spoton_web_server_tcp_server> m_http;
104  QPointer<spoton_web_server_tcp_server> m_https;
105  QTimer m_generalTimer;
106 
107  private slots:
108  void slotHttpClientConnected(const qint64 socketDescriptor);
109  void slotHttpThreadFinished(void);
110  void slotHttpsClientConnected(const qint64 socketDescriptor);
111  void slotHttpsThreadFinished(void);
112  void slotTimeout(void);
113 };
114 
115 class spoton_web_server_thread: public QThread
116 {
117  Q_OBJECT
118 
119  public:
120  spoton_web_server_thread(QAtomicInt *atomicInt,
121  QObject *parent,
122  const QPair<QByteArray, QByteArray> &credentials,
123  const qint64 socketDescriptor):QThread(parent)
124  {
125  m_abort = atomicInt;
126  m_credentials = credentials;
127  m_socketDescriptor = socketDescriptor;
128  }
129 
130  protected:
131  void run(void);
132 
133  private:
134  QAtomicInt *m_abort;
135  QPair<QByteArray, QByteArray> m_credentials;
136  qint64 m_socketDescriptor;
137  void process(const QPair<QByteArray, QByteArray> &credentials,
138  const qint64 socketDescriptor);
139  void process(QSslSocket *socket,
140  const QByteArray &data,
141  const QPair<QString, QString> &address);
142  void processLocal(QSslSocket *socket, const QByteArray &data);
143  void write(QSslSocket *socket, const QByteArray &data);
144  void writeDefaultPage(QSslSocket *socket, const bool redirect = false);
145 };
146 
147 #endif
spoton_web_server_thread
Definition: spot-on-web-server.h:115
spoton_web_server
Definition: spot-on-web-server.h:89
spoton_web_server_tcp_server
Definition: spot-on-web-server.h:38